package com.sensePlusVR.utils;

import java.io.File;


public class DownloadOneUrl implements S3Download.Listener{
	private String TAG="DownloadOneUrl";

	public static final int Finished = 1;
	public static final int Inited = 2;
	public static final int Error = 3;
	public static final int InProgress = 4;
	
	@SuppressWarnings("unused")
	private long fileLen = 0;
	
	private int progress = 0;
	private static int speed = 0;
	private String url;
	private String savePath;
	private String fileName;
	private boolean finished = false;
	private boolean error = false;
	private DownloadCallBack callBack;
	private Object backObj;
	private S3Download download;
	private long totelLen;
	private long doneLen;
	public void setCallBack(DownloadCallBack callBack, Object o){
		this.callBack = callBack;
		backObj = o;
	}
	
	public DownloadOneUrl(String url, String savePath){
		this.url = url;
		this.savePath = savePath;
		String split[] = url.split("/");
		String fileName = split[split.length-1];
		this.fileName = fileName;
		finished = false;
	}
	
	public String getUrl(){
		return url;
	}
	
	public int getSpeed(){
		return speed;
	}
	public static void ResetSpeed(){
		speed = 0;
	}
	public boolean getError(){
		return error;
	}
	
	public void setError(boolean error){
		this.error = error;
	}
	public long getProgressDirect(){
		if (download == null)
			return 0;
		if (progress == 100)
			return progress;
		return download.getProgress();
	}
	
	public long getTotelLen(){
		return totelLen;
	}
	
	public long getDoneLen(){
		if (progress == 100)
			return totelLen;
		return doneLen;
	}
	
	public long getProgress(){
		return progress;
	}
	public boolean isFinished(){
		return finished;
	}
	
	public void startPre(){
		File file = new File(savePath+"/"+fileName);
		if (file.exists()){
			finished = true;
			progress = 100;
			totelLen = doneLen = file.length();
			MyLog.e(TAG, "file "+fileName+"is exists");
			return;
		}
		 
		S3Download temp = new S3Download(url, savePath);
		temp.downloadPre();
		
		totelLen = temp.getTotalLen();
		doneLen = temp.getDoneLen();
	}
	
	public void start(){
		
		finished = false;
		progress = 0;
		File file = new File(savePath+"/"+fileName);
		if (file.exists()){
			finished = true;
			progress = 100;
			if (callBack != null){
				callBack.progress(Finished, (int)100, false, backObj);
			}
			MyLog.e(TAG, "file "+fileName+"is exists"+" error="+error);
			return;
		}
		download = new S3Download(url, savePath);

		File dir = new File(savePath);
		if (!dir.exists()) {
			//MyLog.d(TAG, "file mkdir");
			dir.mkdirs();
		}
		MyLog.e(TAG, "url : " + url);
		MyLog.e(TAG, "savePath :" + savePath);
		download.setListener(this, this);
		download.start();
	}
	
	public void run(){
		
		finished = false;
		progress = 0;
		File file = new File(savePath+"/"+fileName);
		if (file.exists()){
			finished = true;
			progress = 100;
			if (callBack != null){
				callBack.progress(Finished, (int)100, false, backObj);
			}
			MyLog.e(TAG, "file "+fileName+"is exists");
			return;
		}
		S3Download download = new S3Download(url, savePath);

		File dir = new File(savePath);
		if (!dir.exists()) {
			//MyLog.d(TAG, "file mkdir");
			dir.mkdirs();
		}
		MyLog.e(TAG, "url : " + url);
		MyLog.e(TAG, "savePath :" + savePath);
		//download.setListener(this, this);
		download.run();
		
	
	}
	/**
	 * 取消下载
	 */
	public void cancelDownload(){
		
		if(download!=null){
			
			download.cancelDownload();
		}
	}
	
	public interface DownloadCallBack {
		void progress(int event, int progress, boolean error, Object o);
	}


	@Override
	public void notify(int event, long data, Object o) {
		// TODO Auto-generated method stub
		//boolean error = false;
		int state=Inited;
		//int data;
		DownloadOneUrl d = (DownloadOneUrl)o;
		switch(event){
		case S3Download.DownloadEventInited:
			d.fileLen = data;
			state = Inited;
			break;
		case S3Download.DownloadEventFinished:
			MyLog.e(TAG, "Url:"+ d.getUrl() +" progress = "+ 100);
			d.finished = true;
			d.doneLen = download.getDoneLen();
			state = Finished;
			break;
		case S3Download.DownloadEventProgress:
			d.progress = (int)data;
			d.doneLen = download.getDoneLen();
			state = InProgress;
			//MyLog.e(TAG, "Url:"+ d.getUrl() +" progress = "+ d.progress);
			break;
		case S3Download.DownloadEventError:
			MyLog.e(TAG, "download event error");
			d.error = true;
			d.finished = true;
			//d.doneLen = download.getDoneLen();
			state = Error;
			break;
		case S3Download.DownloadEventSpeed:
			speed = (int)data;
			break;
		}
		if (d.callBack != null){
			d.callBack.progress(state, (int)data, d.error, backObj);
		}
	}

}
